# -*- coding: utf-8 -*-
import hashlib
import time

import requests


def send_msg(msg_text):
    random = 104556
    url = "https://live.kewail.com/sms/v1/sendsinglesms?accesskey={0}&random={1}".format("5da2e35587b65f6c3629cef9",
                                                                                         random)  # accesskey id
    print(url)
    dt = "2019-01-03 20:29:29"
    time_unix = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
    # print(time_unix)
    # print(type(time_unix))
    sig_not_sha = "secretkey={0}&random={1}&time={2}&mobile={3}".format("42c78dbb7e0b4caaadb48c635c382636", random,
                                                                        str(time_unix), "13505182805")
    hash = hashlib.sha256()
    hash.update(sig_not_sha.encode("utf-8"))
    sig = hash.hexdigest()
    # print(sig)
    data = {
        "tel": {
            "nationcode": "86",
            "mobile": "13505182805"
        },
        "type": 0,
        "msg": '【Kewail科技】' + msg_text,
        "sig": sig,
        "time": time_unix,
        "extend": "",
        "ext": ""
    }
    # print(data)
    f = requests.post(url, json=data)

    print(f.text)
    return f.text


def send_msg_by_model_DLT(msg_dic):
    """
    通过模板进行传递数据，先测试以下还是用一个msg，能够正常发送数据再说
    :param msg_text:
    :return:
    """
    random = 104556
    url = "https://live.kewail.com/sms/v2/sendsinglesms?accesskey={0}&random={1}".format("5da2e35587b65f6c3629cef9",
                                                                                         random)  # accesskey id
    print(url)
    dt = "2019-01-03 20:29:29"
    time_unix = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
    # print(time_unix)
    # print(type(time_unix))
    sig_not_sha = "secretkey={0}&random={1}&time={2}&mobile={3}".format("42c78dbb7e0b4caaadb48c635c382636", random,
                                                                        str(time_unix), "13505182805")
    hash = hashlib.sha256()
    hash.update(sig_not_sha.encode("utf-8"))
    sig = hash.hexdigest()
    # print(sig)
    data = {
        "tel": {
            "nationcode": "86",
            "mobile": "13505182805"
        },
        "signId": "5aa7ef278475af0e19b05f5b",
        "templateId": "5da3491b87b65f6c3629d701",  # ID,DLT模板
        "type": 0,
        "params": [
            msg_dic["phase_num"],
            msg_dic["note_num"],
            msg_dic["win_note_num"],
            msg_dic["total_bonus"],
        ],
        "sig": sig,
        "time": time_unix,
        "extend": "",
        "ext": ""
    }
    # print(data)
    f = requests.post(url, json=data)

    print(f.text)
    return f.text


def send_msg_by_model_SSQ(msg_dic):
    """
    通过模板进行传递数据，先测试以下还是用一个msg，能够正常发送数据再说
    :param msg_text:
    :return:
    """
    random = 104556
    url = "https://live.kewail.com/sms/v2/sendsinglesms?accesskey={0}&random={1}".format("5da2e35587b65f6c3629cef9",
                                                                                         random)  # accesskey id
    print(url)
    dt = "2019-01-03 20:29:29"
    time_unix = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
    # print(time_unix)
    # print(type(time_unix))
    sig_not_sha = "secretkey={0}&random={1}&time={2}&mobile={3}".format("42c78dbb7e0b4caaadb48c635c382636", random,
                                                                        str(time_unix), "13505182805")
    hash = hashlib.sha256()
    hash.update(sig_not_sha.encode("utf-8"))
    sig = hash.hexdigest()
    # print(sig)
    data = {
        "tel": {
            "nationcode": "86",
            "mobile": "13505182805"
        },
        "signId": "5aa7ef278475af0e19b05f5b",
        "templateId": "5da3496087b65f6c3629d70f",  # ID,SSQ模板
        "type": 0,
        "params": [
            msg_dic["phase_num"],
            msg_dic["note_num"],
            msg_dic["win_note_num"],
            msg_dic["total_bonus"],
        ],
        "sig": sig,
        "time": time_unix,
        "extend": "",
        "ext": ""
    }
    # print(data)
    f = requests.post(url, json=data)

    print(f.text)
    return f.text